package com.wn.ziroombackend.controller;


import com.github.pagehelper.PageInfo;
import com.wn.ziroombackend.entity.Incomeorder;
import com.wn.ziroombackend.entity.Room;
import com.wn.ziroombackend.service.IncomeorderService;
import com.wn.ziroombackend.utils.R;
import com.wn.ziroombackend.utils.RoomCondition;
import com.wn.ziroombackend.vo.IncomeVo;
import com.wn.ziroombackend.vo.OwnerVo;
import com.wn.ziroombackend.vo.RoomVoCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-27 10:56:29
 */
@RestController
@RequestMapping("/income")
public class IncomeorderController {

    @Autowired
    IncomeorderService incomeorderService;

    @PostMapping("/list")
    public R list(
            @RequestParam(defaultValue = "1") Integer num,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestBody RoomVoCondition roomVoCondition
    ) {


        PageInfo<IncomeVo> incomeList = incomeorderService.getIncomeList(num, size, roomVoCondition.getQuartersName(), roomVoCondition.getOwnerName(),roomVoCondition.getStatus());

        return R.ok("ok").data("incomeList", incomeList);

    }

    @PutMapping("/edit")
    public R edit(@RequestBody Incomeorder incomeorder){

        incomeorderService.updateStatusById(incomeorder);
        return  R.ok("ok");
    }

    @PutMapping("/editNow")
    public R editNow(){
        List<Incomeorder> incomeorderList=incomeorderService.selectall();
        for (Incomeorder incomeorder : incomeorderList) {
            if (new Date().getTime()>=incomeorder.getEndTime().getTime()){
                incomeorderService.updateStatusById(incomeorder);
            }
        }
        return  R.ok("ok");
    }

    @PutMapping("/statusMore")
    public R statusMore(String ids){
        String[] split = ids.split(",");
        for (String s : split) {
            int i = Integer.parseInt(s);
            incomeorderService.updateByids(i);
        }
        return  R.ok("ok");
    }
}

